package com.cyzy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cyzy.dto.ClassDO;
import com.cyzy.dto.RequestDTO;
import com.cyzy.dto.User;
import com.cyzy.dto.UserSignIn;
import com.cyzy.mapper.CameraMapper;
import com.cyzy.service.CameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/camera")
public class CameraController {

    @Autowired
    private CameraService cameraService;

    @RequestMapping("hello")
    public String hello()
    {
        return "hello world";
    }

    //人脸对比的接口
    @PostMapping("/compare-faces")
    public ResponseEntity<String> compareFaces(
            @RequestParam("image1") MultipartFile image1,
            @RequestParam("image2") MultipartFile image2) {

        try {
            double score = cameraService.compareFaceImages(image1, image2);
            // 输出结果
            System.out.println("人脸相似度：" + score);
            if (score >= 80){
                return ResponseEntity.ok("人脸对比成功！ 人脸相似度：" + score);
            }
            return ResponseEntity.ok("人脸对比失败！ 人脸相似度："+ score);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("人脸对比失败: " + e.getMessage());
        }
    }
    //人脸签到
    @PostMapping("/sign-in")
    @SentinelResource(value = "signIn",
            blockHandlerClass = SentinelResource.class,
            blockHandler = "handBlock",
            fallbackClass = SentinelResource.class,
            fallback = "handFallBlock")
    public RequestDTO signIn(
            @RequestParam("image") MultipartFile image,
            @RequestParam("userId") String userId,
            @RequestParam("liveName") String liveName) {
        try {
//            double score = cameraService.compareFaceUserId(image, userId);
            System.out.println("userId"+userId+"liveName"+liveName);
            double score = 80;
            if (score >= 80){
                int result =cameraService.updateUserSignIn(userId, 1,liveName);
                if (result > 0){
                    return new RequestDTO(200,"签到成功！",score);
                }else {
                    return new RequestDTO(500,"签到失败！",null);
                }
            }
            return new RequestDTO(500,"签到失败！",score);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"签到失败！",e.getMessage());
        }
    }
    //人脸监控
    @PostMapping("/monitor-faces")
    public RequestDTO monitorFaces(
            @RequestParam("image") MultipartFile image,
            @RequestParam("userId") String userId
    ){
        try {
            double score = cameraService.compareFaceUserId(image, userId);
            if (score >= 75){
                return new RequestDTO(200,"监控成功！",score);
            }
            return new RequestDTO(500,"监控失败！",score);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"监控失败！",e.getMessage());
        }
    }
    //添加人脸
    @PostMapping("/add-face")
    public RequestDTO addFace(
            @RequestParam("image") MultipartFile image,
            @RequestParam("userId") String userId) {
        try {
            String faceToken = cameraService.addFace(image, userId);
            if (faceToken != null){
                return new RequestDTO(200,"录入人脸成功！",faceToken);
            }
            return new RequestDTO(500,"录入人脸失败！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"录入人脸失败！",e.getMessage());
        }
    }


    //获取签到信息
    @RequestMapping("/get-sign-in")
    public RequestDTO getSignIn(
            @RequestParam("liveName") String liveName
             ){
        try {
            List<User> userSignIn = cameraService.getUserSignIn(liveName);
            if (userSignIn != null){
                return new RequestDTO(200,"获取签到信息成功！",userSignIn);
            }
            return new RequestDTO(500,"获取签到信息失败！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"获取签到信息失败！",e.getMessage());
        }
    }
    //更新签到信息
    @RequestMapping("/update-sign-in")
    public RequestDTO updateSignIn(
            @RequestParam("userId") String userId,
            @RequestParam("signStatues") int signStatues,
            @RequestParam("liveName") String liveName
    ){
        try {
            int result = cameraService.updateUserSignIn(userId, signStatues,liveName);
            if (result > 0){
                return new RequestDTO(200,"更新签到信息成功！",null);
            }
            return new RequestDTO(500,"更新签到信息失败！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"更新签到信息失败！",e.getMessage());
        }
    }
    //标记为未签到
    @RequestMapping("/mark-as-not-sign-in")
    public RequestDTO markAsNotSignIn(
            @RequestParam("userId") String userId,
            @RequestParam("liveName") String liveName
    ){
        try {
            int result = cameraService.updateUserSignIn(userId,0,liveName);
            if (result > 0){
                return new RequestDTO(200,"标记为未签到成功！",null);
            }
            return new RequestDTO(500,"标记为未签到失败！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"标记为未签到失败！",e.getMessage());
        }

    }
    //获取所有班级信息
    @RequestMapping("/get-all-class")
    public RequestDTO getAllClass(){
        try {
            List<ClassDO> classList = cameraService.getAllClass();
            if (classList != null){
                return new RequestDTO(200,"获取所有班级信息成功！",classList);
            }
            return new RequestDTO(500,"获取所有班级信息失败！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"获取所有班级信息失败！",e.getMessage());
        }
    }
    //获取所有学生信息
    @RequestMapping("/get-all-students")
    public RequestDTO getAllStudents(
            @RequestParam("classId") String classId,
            @RequestParam("liveName") String liveName
    ){
        try {
            List<User> students = cameraService.getAllStudents(classId,liveName);
            if (students != null){
                return new RequestDTO(200,"获取所有学生信息成功！",students);
            }
            return new RequestDTO(500,"获取所有学生信息失败！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new RequestDTO(500,"获取所有学生信息失败！",e.getMessage());
        }
    }
}
